(define-module (builders gallery-builder)
  #:use-module (theme theme)
  #:use-module (haunt site)
  #:use-module (haunt post)
  #:use-module (haunt page)
  #:use-module (haunt html)
  #:use-module (haunt utils)
  #:use-module (utils utils)
  #:use-module (ice-9 match)
  #:use-module (ice-9 ftw)
  #:declarative? #f
  #:export (gallery-builder))

;; my pages builder cannot render pages the same way that my-blog
;; builder does.  my blog builder renders posts, because it gets a
;; list of posts.  However, my pages are NOT posts.

;; Builders are procedures that return one or more page objects (*note
;; Pages::) when applied.  A builder accepts two arguments: A site
;; (*note Sites:: and a list of posts (*note Posts::).

(define remove-stat
  ;; Remove the `stat' object the `file-system-tree' provides
  ;; for each file in the tree.
  (match-lambda
    ((name stat)              ; flat file
     name)
    ((name stat children ...) ; directory
     (list name (map remove-stat children)))))

(define (gallery-images-list)
  (car
   (cdr
    (let ([dir "./images/gallery/"])
      (remove-stat (file-system-tree dir))))))

(define (gallery-images-html list)
  (let loop ([list list])
    (if (null? list)
        '()
        `((img (@ (src ,(string-append "./images/gallery/" (car list)) )))
          ,(loop (cdr list))))))

(define (gallery-builder)
  (lambda (site posts)
    (make-page "gallery.html"
               (gnucode-layout site "Gallery"
                               `((div (@ (class "gallery-flex"))
                                      ,(gallery-images-html (gallery-images-list))
                                      )))
               sxml->html)))
